<template>
  <a-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item label="户主姓名" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'householdName', validatorRules.householdName]" placeholder="请输入户主姓名"></a-input>
        </a-form-item>
        <a-form-item label="身份证号" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <!--          <a-input-number v-decorator="[ 'idNumber', validatorRules.idNumber]" placeholder="请输入身份证号" style="width: 100%"/>-->
          <a-input v-decorator="[ 'idNumber', validatorRules.idNumber]" placeholder="请输入身份证号"></a-input>
        </a-form-item>
        <!-- <a-form-item label="乡镇" :labelCol="labelCol" :wrapperCol="wrapperCol">
           <j-dict-select-tag type="list" v-decorator="['fwzId']" :trigger-change="true" dictCode="tb_jrtx_fwz,township_name,id" placeholder="请选择乡镇"/>
         </a-form-item>
         <a-form-item label="自然村" :labelCol="labelCol" :wrapperCol="wrapperCol">
           <j-dict-select-tag type="list" v-decorator="['fwbId']" :trigger-change="true" dictCode="tb_jrtx_fwb,service_name,id" placeholder="请选择自然村"/>
         </a-form-item>-->
        <a-form-item label="乡镇和自然村" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-cascader
            :options="options"
            style="width: 100%"
            @change="onChange"
            :value="cascaderValue"
            placeholder="请输入乡镇和自然村"
          />
        </a-form-item>
          <a-form-item label="家庭地址" :labelCol="labelCol" :wrapperCol="wrapperCol">
            <a-input v-decorator="[ 'address', validatorRules.address]" placeholder="请输入家庭地址"></a-input>
          </a-form-item>
        <!--        0_AAA+,1_AAA,2_AA,3_A,4_无信等级-->
        <a-form-item label="信用等级" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag type="list" v-decorator="['creditRating',validatorRules.creditRating]" :trigger-change="true" dictCode="credit_rating"
                             placeholder="请选择信用等级"/>
        </a-form-item>

        <a-form-item label="信用评分" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-tooltip>
            <template slot="title">
              范围：1~100
            </template>
            <a-input v-decorator="[ 'creditScore', validatorRules.creditScore]" placeholder="请输入信用评分"></a-input>
          </a-tooltip>
        </a-form-item>
        <!--        0_贫困，1_非贫困-->
        <a-form-item label="是否贫困" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag type="list" v-decorator="['isPoor',validatorRules.isPoor]" :trigger-change="true" dictCode="is_poor"
                             placeholder="请选择是否贫困"/>
        </a-form-item>
        <!--        0_脱贫仍享受政策、1_未脱贫、2_稳定脱贫-->
        <a-form-item label="贫困户状态" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag type="list" v-decorator="['poorHouseholdStatus',validatorRules.poorHouseholdStatus]" :trigger-change="true"
                             dictCode="poor_household_status" placeholder="请选择贫困户状态"/>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>

    import {httpAction,getAction} from '@/api/manage'
    import pick from 'lodash.pick'
    import JDictSelectTag from "@/components/dict/JDictSelectTag"

    export default {
        name: "CreditFarmersModal",
        components: {
            JDictSelectTag,
        },
        data() {
            return {
                cascaderValue: [],
                defaultValue: [],
                options: [],
                form: this.$form.createForm(this),
                title: "操作",
                width: 800,
                visible: false,
                model: {},
                labelCol: {
                    xs: {span: 24},
                    sm: {span: 5},
                },
                wrapperCol: {
                    xs: {span: 24},
                    sm: {span: 16},
                },
                confirmLoading: false,
                validatorRules: {
                    idNumber: {rules: [{ required: true, message: '身份证号不能空'}]},
                    householdName: {rules: [{ required: true, message: '户主姓名不能空'}]},
                    creditRating: {rules: [{ required: true, message: '信用等级不能空'}]},
                    creditScore: {rules: [{ required: true, message: '信用评分不能空'}]},
                    address: {rules: [{ required: true, message: '家庭地址不能为空'}]},
                    fwzId: {},
                    fwbId: {},
                    isPoor: {rules: [{ required: true, message: '请选择是否贫困'}]},
                    poorHouseholdStatus: {},
                },
                url: {
                    add: "/creditsystem/creditFarmers/add",
                    edit: "/creditsystem/creditFarmers/edit",
                }

            }
        },
        created() {
        },
        methods: {
            onChange(value, selectedOptions) {
                if (value.length > 0) {
                    this.cascaderValue = value
                    this.fwbId = selectedOptions[1].code;
                    this.fwzId = selectedOptions[0].code;
                }
            },
            add() {
                this.edit({});
                this.cascaderValue = []
            },
            edit(record) {
                const that = this
                this.fwzId = record.fwzId;
                this.fwbId = record.fwbId;
                getAction("/financialservices/serviceDepartment/serviceDepartMentCascadedDropDown", "").then((res) => {
                    if (res.success) {
                        this.options = res.result
                    }
                    this.loading = false;
                })
                this.form.resetFields();
                this.model = Object.assign({}, record);
                this.visible = true;
                this.$nextTick(() => {
                    this.form.setFieldsValue(pick(this.model,'address', 'idNumber', 'householdName', 'creditRating', 'creditScore', 'fwzId', 'fwbId', 'isPoor', 'poorHouseholdStatus'))
                })
                if (JSON.stringify(record) != "{}") {
                    var params = {};
                    params.fwzId = record.fwzId;
                    params.fwbId = record.fwbId;
                    getAction("/financialservices/serviceDepartment/cascadedDropDownEchoResults", params).then((res) => {
                        if (res.success) {
                            that.cascaderValue = res.result
                        }
                        this.loading = false;
                    })
                }

            },
            close() {
                this.$emit('close');
                this.visible = false;
            },
            handleOk() {
                const that = this;
                // 触发表单验证
                this.form.validateFields((err, values) => {
                    if (!err) {

                        let httpurl = '';
                        let method = '';
                        if (!this.model.id) {
                            httpurl += this.url.add;
                            method = 'post';
                        } else {
                            httpurl += this.url.edit;
                            method = 'put';
                        }
                        values.fwbId = this.fwbId;
                        values.fwzId = this.fwzId;
                        if (!values.fwbId) {
                            that.$message.warning("所属自然村不能为空");
                            return ;
                        }
                        if (!values.fwzId) {
                            that.$message.warning("所属乡镇不能为空");
                            return ;
                        }
                        that.confirmLoading = true;
                        let formData = Object.assign(this.model, values);
                        console.log("表单提交数据", formData)
                        httpAction(httpurl, formData, method).then((res) => {
                            if (res.success) {
                                that.$message.success(res.message);
                                that.$emit('ok');
                            } else {
                                that.$message.warning(res.message);
                            }
                        }).finally(() => {
                            that.confirmLoading = false;
                            that.close();
                        })
                    }
                })
            },
            handleCancel() {
                this.close()
            },
            popupCallback(row) {
                this.form.setFieldsValue(pick(row,'address', 'idNumber', 'householdName', 'creditRating', 'creditScore', 'fwzId', 'fwbId', 'isPoor', 'poorHouseholdStatus'))
            }

        }
    }
</script>